const mongoose = require('mongoose')

//连接mongo
let options = {
  database:'mongodb://hmy:123456@localhost:27017/we_teach',
  // database:'mongodb://@localhost:27017/we_teach',
  dataConfig:{useNewUrlParser:true,useUnifiedTopology: true}
}
mongoose.connect(options.database,options.dataConfig)
.then(
  ()=>{
    console.log("连接成功")
  }
).catch(err=>{
  console.log("连接失败")
  console.log(err)
})
// 操作表（集合），定义一个schema,每一个schema映射一个表,字段和表一一对应
// 定义scheme
var student_schema = mongoose.Schema({
  user:{
    type:String,
    unique:true//设置唯一索引，index:true为普通索引
  },
  pass:String,
  student:String,
  avatar:String,
  sex:String,
  subject_good:String,
})
//teacher's scheme
var teacher_schema = mongoose.Schema({
  user:{
    type:String,
    unique:true//设置唯一索引，index:true为普通索引
  },
  pass:String,
  teacher:String,
  avatar:String,
  sex:String,
  subject_good:String,
  school_name:String
})

//article's scheme
var article_schema = mongoose.Schema({
  id: String,
  title: String,
  article: Array,
  author: String,
  source: String,
  time: String,
})

//real_time_info's scheme
var real_time_info_schema = mongoose.Schema({
  id: String,
  title: String,
  content: Array, //文章内容
  tag: Array,
  readingCount:Number//阅读数
})

//policy's scheme
var policy_schema = mongoose.Schema({
  id: String,
  title: String, //文章名称
  content: Array, //文章内容
  tag: Array,
  readingCount:Number//阅读数
})

//subject's scheme
var subject_schema = mongoose.Schema({
  id: String,
  title: String,
  article: Array,
  img:String,
  author: String,
  source: String,
  time: String,
  tag:Array
})

//test's scheme
var test_schema = mongoose.Schema({
  id: String,
  title: String, //文章名称
  content: Array, //文章内容
  tag: Array
})


//school's scheme
var school_schema = mongoose.Schema({
  id: String,
  school: String, //学校名称
  school_img: String, //学校图片
  school_brief: String,
  campus_brief: Array,
  corner_img: Array,
  tag: Array
})


var latest_releases_schema = mongoose.Schema({
  id: String,
  title: String, //文章名称
  sub_title:Array,//子标题
  content: Array, //文章内容
  time:String,//时间
  author:String,//作者
  tag: Array,
  readingCount:Number//阅读数
})

// 把scheme转化为model，即创建数据库表模型，操作表teachers表,首字母要大写
// students’ model
var Students = mongoose.model('students',student_schema)

// teacher’ model
var Teachers = mongoose.model('teachers',teacher_schema)
//article's scheme
var Articles = mongoose.model('nous_articles',article_schema)
//real_time_info_schema
var realtime_infos = mongoose.model('real_time_infos',real_time_info_schema)
// policy_schema
var policies = mongoose.model('policies',policy_schema)
// subject_schema
var subjects = mongoose.model('subject_articles',subject_schema)
// test's scheme
var tests = mongoose.model('tests',test_schema)
//school's scheme
var schools = mongoose.model('schools',school_schema)
var latest_releases = mongoose.model('latest_releases',latest_releases_schema)
// 导出
module.exports={
  Students : Students,//学生用户
  Teachers : Teachers,//老师用户
  Articles:Articles,//常识
  realtime_infos:realtime_infos,//实时资讯
  policies:policies,//政策
  subjects:subjects,//课程文章
  tests:tests,//试题库
  schools:schools,//学校,
  latest_releases:latest_releases
}